package cn.ctrl;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import cn.bean.Play;
import cn.bean.User;
import cn.service.TicketService;
import cn.service.impl.TicketServiceImpl;

public class PlayCtrl implements ActionListener {

	
	
	
	private Play play;
	private List<JButton>  seatButton;
	private JLabel infoLabel;
	private User user;
	private int n;
	private JFrame frame;
	private JPanel panel2;
private String day;



	public PlayCtrl(Play play, List<JButton> seatButton, JLabel infoLabel,
			User user, int i, JFrame frame,String day) {
		super();
		this.play = play;
		this.seatButton = seatButton;
		this.infoLabel = infoLabel;
		this.user = user;
		this.n = i;
		this.frame = frame;
		this.day=  day;
	}




	@Override
	public void actionPerformed(ActionEvent e) {
		TicketService ts = new TicketServiceImpl();
		
		int[][] i = new int[4][4];
		
		i = ts.seatStatus(play);
		
		infoLabel.setText(play.getFilm_info().getName()+"/"+play.getPrice()+"Ԫ/"+play.getEdition());
		infoLabel.setBounds(225, 60, 200, 30);
		
		
		int l = 0;
		for(int k = 0;k<5;k++){
			if(i[0][k]==0){
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(true);
				l++;
			}else{
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(false);
				l++;
			}
		}
		for(int k = 0;k<5;k++){
			if(i[1][k]==0){
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(true);
				l++;
			}else{
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(false);
				l++;
			}
		}
		for(int k = 0;k<5;k++){
			if(i[2][k]==0){
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(true);
				l++;
			}else{
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(false);
				l++;
			}
		}
		for(int k = 0;k<5;k++){
			if(i[3][k]==0){
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(true);
				l++;
			}else{
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(false);
				l++;
			}
		}
		for(int k = 0;k<5;k++){
			if(i[4][k]==0){
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(true);
				l++;
			}else{
				seatButton.get(l).setVisible(true);
				seatButton.get(l).setEnabled(false);
				l++;
			}
			
			   for(JButton a:seatButton){
				  try{
					  a.removeActionListener(a.getActionListeners()[0]);
				  }catch(Exception e1){
					  
				  }
			    }
			   
			   
			   int n=0;
			   for(JButton a:seatButton){
				   a.addActionListener(new BuyCtrl(user,play,n,frame,day));
				   n++;
			    }
			
			
			   
		}
	}

}
